<?php   

class Email_Colleague extends Controller {

	function Email_Colleague()
	{
		parent::Controller();	                         
	}
	
    function index($type=NULL, $id=NULL)
    {                        
		if ($type==NULL || $id==NULL) show_404();            
	
		$this->load->library("form_validation"); 		

		if($this->input->post("id") !== FALSE){
			
			$this->_process_email_colleague_form($type);
		
		}

		$data['token'] = generate_token("email_colleague",20);
		$data['type'] = $type; 
		
		switch ($type){
			
			case 'news':				
				$this->load->model('newsmodel'); 
				$item = $this->newsmodel->get_item($id);
				if (empty($item)) show_404();
				$data["id"] = $item["id_News"];
				$data["date"] = $item["Date_News"];
				$data["title"] = $item["Title_News"];
				$data["text"] = $item["Introduction_News"];  
			break;
			
			case 'publicity':				
				$this->load->model('publicitymodel'); 
				$item = $this->publicitymodel->get_item($id);
				if (empty($item)) show_404();
				$data["id"] = $item["id_Publicity"];
				$data["date"] = $item["Date_Publicity"];
				$data["title"] = $item["Title_Publicity"];
				$data["text"] = $item["Introduction_Publicity"];  
			break;
			
			case 'glossary':				
				$this->load->model('glossarymodel'); 
				$item = $this->glossarymodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Glossary"];
				$data["title"] = $item["Title_Glossary"];
				$data["text"] = $item["Introduction_Glossary"]; 
			break;
			
			case 'faq':				
				$this->load->model('faqmodel'); 
				$item = $this->faqmodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_FAQs"];
				$data["title"] = $item["Title_FAQs"];
				$data["text"] = word_limiter($item["Description_FAQs"], 20); 
			break;
			
			case 'articles':				
				$this->load->model('articlesmodel'); 
				$item = $this->articlesmodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Articles"];
				$data["title"] = $item["Title_Articles"];
				$data["text"] = $item["Introduction_Articles"];  
			break;
			
			case 'exhibitions':				
				$this->load->model('exhibitionsmodel'); 
				$item = $this->exhibitionsmodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Exhibitions"];
				$data["date"] = $item["Date_Exhibitions"];
				$data["date2"] = $item["Date2_Exhibitions"];
				$data["title"] = $item["Title_Exhibitions"];
				$data["text"] = $item["Introduction_Exhibitions"];  
			break;
			
			case 'career_opportunities':				
				$this->load->model('careeropportunitiesmodel'); 
				$item = $this->careeropportunitiesmodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Career_Opportunities"];
				$data["title"] = $item["Title_Career_Opportunities"];
				$data["text"] = word_limiter($item["Description_Career_Opportunities"], 20); 
			break;
			
			case 'products':				
				$this->load->model('productsmodel'); 
				$item = $this->productsmodel->get_product($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Products"];
				$data["image"] = ($item['Main_Image']!="") ? base_url().UPLOADS_FOLDER.PRODUCT_IMG_FOLDER.$item['Main_Image'] : "";
				$data["title"] = $item["Name_Products"];
				$data["text"] = $item["Introduction_Products"]; 
			break;
			
			case 'portfolio':				
				$this->load->model('portfoliomodel'); 
				$item = $this->portfoliomodel->get_item($id);
				if (empty($item)) show_404(); 				
				$data["id"] = $item["id_Portfolio"];
				$data["image"] = ($item['Main_Image']!="") ? base_url().UPLOADS_FOLDER.PORTFOLIO_IMG_FOLDER.$item['Main_Image'] : "";
				$data["title"] = $item["Name_Portfolio"];
				$data["text"] = $item["Introduction_Portfolio"]; 
			break;
			
			case 'wishlist':
				$this->load->model('wishlistsmodel');
				if (!session_check()) {
					set_referrer();
					redirect('insider', 'refresh');
				}
				// we use id="ALL" for all lists 
				if ($id == "ALL"){
					$data["lists"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'));
				} else {
					$data["lists"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'), $id);
				}
				if (empty($data["lists"])) show_404(); 		
				$data["id"] = $id;
			break;

		}
		
		$data["sender_email"] = "";
		if (session_check()) {
			$user = $this->usermodel->get_user_details($this->session->userdata('user_id'));
			if ($user['Email_UsersDetails'] != "") {
				$data["sender_email"] = $user['Email_UsersDetails'];
			}
		}
		
		$this->load->view('website/default/email_overlay', $data);   
    }	

    private function _process_email_colleague_form($type)
    {	
		if(validate_token("email_colleague",$this->input->post("token",TRUE))){

			 if($this->_validate_email_form()){			 
				if ($this->_send_colleague_email($type, $this->input->post("sender_email",TRUE), $this->input->post("email",TRUE), $this->input->post("message",TRUE), $this->input->post("id",TRUE))){
					// show success
					echo "<h2>".$this->lang->line('EmailColleagueSuccess1')."</h2><p>".$this->lang->line('EmailColleagueSuccess2')."</p>";
					die();
				} else {
					// show error
					echo "<h2>".$this->lang->line('EnquiryError1')."</h2><p>".$this->lang->line('EnquiryError2')."</p>";
					die();
				} 
			}
		}
    }
	private function _send_colleague_email($type, $sender_email, $recipient_email, $message, $id){		
					
		switch ($type){
			case 'news':
				$this->load->model('newsmodel');
				$item = $this->newsmodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_News']);
				$data['link'] = site_url('news/view/'.$item['id_News']);
				$data['title'] = $item['Title_News'];
				$data['date'] =  format_date($item["Date_News"]);
				$data['image'] = $item['Image_News']!="" ? base_url().UPLOADS_FOLDER.NEWS_IMG_FOLDER.$item['Image_News'] : "";
				$data['introduction'] = $item['Introduction_News'];
				$data['description'] = $item['Description_News'];
			break;
			
			case 'publicity':
				$this->load->model('publicitymodel');
				$item = $this->publicitymodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_Publicity']);
				$data['link'] = site_url('publicity/view/'.$item['id_Publicity']);
				$data['title'] = $item['Title_Publicity'];
				$data['date'] =  format_date($item["Date_Publicity"]);
				$data['image'] = $item['Image_Publicity']!="" ? base_url().UPLOADS_FOLDER.NEWS_IMG_FOLDER.$item['Image_Publicity'] : "";
				$data['introduction'] = $item['Introduction_Publicity'];
				$data['description'] = $item['Description_Publicity'];
			break;
			
			case 'glossary':
				$this->load->model('glossarymodel');
				$item = $this->glossarymodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_Glossary']);
				$data['link'] = site_url('glossary/view/'.$item['id_Glossary']);
				$data['title'] = $item['Title_Glossary'];
				$data['image'] = $item['Image_Glossary']!="" ? base_url().UPLOADS_FOLDER.GLOSSARY_IMG_FOLDER.$item['Image_Glossary'] : "";
				$data['introduction'] = $item['Introduction_Glossary'];
				$data['description'] = $item['Description_Glossary'];
			break;
			
			case 'faq':
				$this->load->model('faqmodel');
				$item = $this->faqmodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_FAQs']);
				$data['link'] = site_url('faq');
				$data['title'] = $item['Title_FAQs'];
				$data['description'] = $item['Description_FAQs'];
			break;
			
			case 'articles':
				$this->load->model('articlesmodel');
				$item = $this->articlesmodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_Articles']);
				$data['link'] = site_url('articles/view/'.$item['id_Articles']);
				$data['title'] = $item['Title_Articles'];
				$data['image'] = $item['Image_Articles']!="" ? base_url().UPLOADS_FOLDER.ARTICLES_IMG_FOLDER.$item['Image_Articles'] : "";
				$data['introduction'] = $item['Introduction_Articles'];
				$data['description'] = $item['Description_Articles'];
			break;
			
			case 'exhibitions':
				$this->load->model('exhibitionsmodel');
				$item = $this->exhibitionsmodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_Exhibitions']);
				$data['link'] = site_url('exhibitions/view/'.$item['id_Exhibitions']);
				$data['title'] = $item['Title_Exhibitions'];
				$data['date'] =  format_date($item["Date_Exhibitions"]);
				$data['date2'] =  format_date($item["Date2_Exhibitions"]);
				$data['image'] = $item['Image_Exhibitions']!="" ? base_url().UPLOADS_FOLDER.EXHIBITIONS_IMG_FOLDER.$item['Image_Exhibitions'] : "";
				$data['introduction'] = $item['Introduction_Exhibitions'];
				$data['description'] = $item['Description_Exhibitions'];
			break;
			
			case 'career_opportunities':
				$this->load->model('careeropportunitiesmodel');
				$item = $this->careeropportunitiesmodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_article'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Title_Career_Opportunities']);
				$data['link'] = site_url('career_opportunities');
				$data['title'] = $item['Title_Career_Opportunities'];
				$data['description'] = $item['Description_Career_Opportunities'];
			break;
			
			case 'products':
				$this->load->model('productsmodel');
				$item = $this->productsmodel->get_product($id);
				$template_file = 'templates/emails/email_colleague_product'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Name_Products']);
				$data['link'] = site_url('products/view/'.$item['id_Products']);
				$data['title'] = $item['Name_Products'];
				$data['image'] = ($item['Main_Image']!="") ? base_url().UPLOADS_FOLDER.PRODUCT_IMG_FOLDER.$item['Main_Image'] : "";
				$data['introduction'] = $item['Introduction_Products'];
				$data['description'] = $item['Description_Products'];
				$data["applications"] = $this->productsmodel->get_product_applications($id); 
				$data["specifications"] = $this->productsmodel->get_product_specifications($id);
				$data["specifications"][] = array( "Name_Specs" => "Finishes", "Value" => $this->productsmodel->finishes_result_into_string($this->productsmodel->get_product_finishes($id)) );
			break;
			
			case 'portfolio':
				$this->load->model('portfoliomodel');
				$item = $this->portfoliomodel->get_item($id);
				$template_file = 'templates/emails/email_colleague_product'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($item['Name_Portfolio']);
				$data['link'] = site_url('portfolio/view/'.$item['id_Portfolio']);
				$data['title'] = $item['Name_Portfolio'];
				$data['image'] = ($item['Main_Image']!="") ? base_url().UPLOADS_FOLDER.PORTFOLIO_IMG_FOLDER.$item['Main_Image'] : "";
				$data['introduction'] = $item['Introduction_Portfolio'];
				$data['description'] = $item['Description_Portfolio'];
				$data["products_used"] = $this->portfoliomodel->get_products($id); 
			break;
			
			case 'wishlist':
				$this->load->model('wishlistsmodel');
				// we use id="ALL" for all lists 
				if ($id == 'ALL'){
					$data['wishlists'] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'));
				} else {
					$data['wishlists'] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'), $id);	
				}
				
				$data['user'] = $this->usermodel->get_user_details($this->session->userdata('user_id'));
				$template_file = 'templates/emails/email_colleague_wishlist'.INSTANCE_FILE_SUFFIX;
				$subject = strip_tags($data['user']['FirstName_UsersDetails'])."'s wish list";
				$data['link'] = site_url();
			break;
		}		

		// load common data			
		//$user = $this->usermodel->get_user_details($this->session->userdata('user_id'));
		$data['message'] = nl2br($message);

		// load email template
		$email_body = $this->load->view($template_file, $data, TRUE);
		//die($email_body);
				
		// send the email
		$this->load->helper('send_email');     
		send_email($sender_email, 
					NULL, 
					$recipient_email,
					$subject, 
					$email_body);
		
		// echo the email raw content right before sending
		//echo $this->email->print_debugger();			
					
		return true;
	}	
	
	private function _validate_email_form(){  
		$this->load->library("form_validation"); 
        $config = array(
                        array(
                         'field'   => 'sender_email',
                         'label'   => 'sender\'s email',
                         'rules'   => 'trim|xss_clean|required|valid_email'
                        ),
                        array(
                         'field'   => 'email',
                         'label'   => 'recipient\'s email',
                         'rules'   => 'trim|xss_clean|required|valid_email'
                        ),
                        array(
                         'field'   => 'message',
                         'label'   => 'message',
                         'rules'   => 'trim|xss_clean'
                        )
                    );

        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); 
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */